﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using XingFuStudio.Pioneer.BusinessLogic;
using XingFuStudio.Pioneer.Container.Interface;
using XingFuStudio.Pioneer.Entity;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Container.Interface.Event;
using System.Xml.Linq;

namespace XingFuStudio.Pioneer.Applications.City {
    /// <summary>
    /// 应用入口
    /// </summary>
    public class AppEntrance : ApplicationBase {
        /// <summary>
        /// 花园管理类对象
        /// </summary>
        private CityManager manager = null;

        /// <summary>
        /// 重写启动方法
        /// </summary>
        /// <param name="accounts"></param>
        public override void Run(List<AccountInfo> accounts) {
            base.Run(accounts);

            //初始化花园管理对象
            this.manager = new CityManager();
            this.manager.PrintLog += new EventHandler<LogEventArgs>(Manager_PrintLog);
            this.manager.Error += new EventHandler<ErrorEventArgs>(Manager_Error);

            //初始化线程
            this.thread = new Thread(this.ThreadMethod);
            this.thread.IsBackground = true;
            //启动线程
            this.thread.Start();
        }

        /// <summary>
        /// 打印日志方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Manager_PrintLog(object sender, LogEventArgs e) {
            this.DoPrintLog(this, e.IsNewLing, e.LogMessage);
        }

        /// <summary>
        /// 出错事件执行方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Manager_Error(object sender, ErrorEventArgs e) {
            this.DoError(this, e.Exception);
        }

        /// <summary>
        /// 线程执行方法
        /// </summary>
        private void ThreadMethod() {
            //初始化Http请求处理类对象
            HttpManager httpManager = new HttpManager();

            //不使用延时
            httpManager.DelayTimes = 0;

            while (this.isRun) {
                //遍历用户
                foreach (AccountInfo account in this.accounts) {
                    try {
                        //尝试登录
                        LoginManager login = new LoginManager(httpManager);
                        LoginResults result = login.Login(account);

                        //判断是否登录成功
                        if (result == LoginResults.Success) {
                            //初始化农场管理类对象
                            this.manager.ChangeAccount(httpManager, account);

                            //打印日志
                            this.DoPrintLog(this, true, "开始自动管理帐户[{0}({1})]的开心城市相关功能。", account.UserName, account.Email);

                            //运行农场管理
                            manager.Run();

                            //打印日志
                            this.DoPrintLog(this, true, "帐户[{0}({1})]的自动管理结束", account.UserName, account.Email);
                            this.DoPrintLog(this, true, "");
                        } else {
                            this.DoPrintLog(this, true, "登录[{0}({1})]失败", account.UserName, account.Email);
                            this.DoLoginFailed(this, account, (int)result, httpManager);
                        }
                    } catch (Exception ex) {
                        this.DoError(this, ex);
                    }
                }

                //线程等待
                for (int i = 0; i < this.interval * 60; i++) {
                    if (this.isRun) {
                        Thread.Sleep(1000);
                    } else {
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 获取持久化信息的XML对象
        /// </summary>
        public override XElement GetPersistenceXML() {
            return base.GetPersistenceXML();
        }

        /// <summary>
        /// 赋值持久化信息的XML对象
        /// </summary>
        /// <param name="xml">持久化的XML</param>
        public override void SetPersistenceXML(XElement xml) {
            base.SetPersistenceXML(xml);
        }
    }
}
